"""
conf:
    ssh/ -> ~/.ssh/
    tinyops.toml: servers, timers, webhooks, checkers
    tasks/xxx.toml
data:
    tinyops.db
logs:
    logs/tinyops.log
    logs/tasks/xxx.log
temp:
    current/config.json
    current/task-xxx.json
    tasks/xxx/
"""
import os
import functools
from pathlib import Path

from .model import Config


def _return_path(func):
    @functools.wraps(func)
    def wrapped(*args, **kwargs):
        p = Path(func(*args, **kwargs))
        os.makedirs(p.parent, exist_ok=True)
        return str(p.absolute())
    return wrapped


def _return_dir(func):
    @functools.wraps(func)
    def wrapped(*args, **kwargs) -> str:
        p = Path(func(*args, **kwargs))
        os.makedirs(p, exist_ok=True)
        return str(p.absolute())
    return wrapped


class DirectoryManager:
    def __init__(self, config: Config):
        self.config = config
        self.temp = Path(config.directory['temp'])
        self.logs = Path(config.directory['logs'])
        self.data = Path(config.directory['data'])

    @_return_path
    def current_config(self) -> str:
        return self.temp / 'current' / 'config.json'

    @_return_path
    def current_task(self, task: str) -> str:
        return self.temp / 'current' / f'task-{task}.json'

    @_return_dir
    def task_dir(self, task: str) -> str:
        return self.temp / 'tasks' / task

    @_return_path
    def task_log(self, task: str) -> str:
        return self.logs / 'tasks' / f'{task}.log'

    @_return_path
    def database(self) -> str:
        return self.data / 'database.db'

    @_return_path
    def queue(self) -> str:
        return self.data / 'queue.db'
